<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between" align="center">
        访问时间段:
        <NDatePicker input-readonly style="width: 280rem" placeholder="请选择访问时间段" clearable type="daterange"
          @update-formatted-value="handleFormattedStart" />
        访问单位/个人:
        <NInput style="width: 180rem" placeholder="请输入访问单位/个人" v-model:value="paginationReactive.accessMan" clearable />
        带队领导/职务:
        <NInput style="width: 180rem" placeholder="请输入带队领导/职务" v-model:value="paginationReactive.duty" clearable />
        联系人姓名:
        <NInput style="width: 160rem" placeholder="请输入联系人姓名" v-model:value="paginationReactive.contacts" clearable />
        联系人联系方式:
        <NInput style="width: 190rem" placeholder="请输入联系人联系方式" v-model:value="paginationReactive.phone" clearable />
        <NButton type="primary" style="width: 78rem" ghost
          @click="() => ((paginationReactive.page = 1), getReceptionList())">
          查询
        </NButton>
      </NSpace>
      <NButton type="primary" style="width: 115rem" @click="$router.push({ path: '/OA/Reception/ReceptionAdd' })">
        接待申请
      </NButton>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :columns="columns" :data="tableData" :pagination="false"
        :loading="tableLoading" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>

  <NModal :auto-focus="false" v-model:show="modalVisible" @close="modalVisible = false">
    <VDialogContent @close="modalVisible = false" @submit="recall" title="撤回提示">
      <div style="width: 300rem; height: 100rem; text-align: center; line-height: 100rem">
        您确定撤回该条接待申请吗？
      </div>
    </VDialogContent>
  </NModal>
</template>
<script setup lang="ts">
import {
  NSpace,
  NButton,
  NDataTable,
  NPagination,
  useMessage,
  NModal,
  NInput,
  NDatePicker
} from 'naive-ui'
import { h, reactive, ref, watch } from 'vue'
import { sendRequest } from '@/utils/api'
import { useRoute } from 'vue-router'
import type { ReceptionModel } from '@/api/reception'
import type { HttpResponse } from '@/utils/http'
import type { DataTableColumns } from 'naive-ui'
import TableView from '@/components/TableView.vue'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'
import router from '@/router'
import { useViewerStore } from '@/views/OA/Common/store'

const useStore = useViewerStore()
const message = useMessage()
const route = useRoute()
const modalVisible = ref(false)

const columns: DataTableColumns = [
  {
    title: '申请人',
    key: 'userName',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '申请部门',
    key: 'deptName',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '申请类型',
    key: 'receptionType',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '访问时间',
    key: 'accessTime',
    width: 80,
    align: useStore.isCenter,
    render({ accessTime }: any) {
      return accessTime.split(' ')[0]
    }
  },
  {
    title: '访问单位/个人',
    key: 'accessMan',
    width: 70,
    align: useStore.isCenter
  },
  {
    title: '带队领导/职务',
    key: 'duty',
    width: 70,
    align: useStore.isCenter
  },
  {
    title: '联系人姓名',
    key: 'contacts',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '联系人联系方式',
    key: 'phone',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '申请时间',
    key: 'createTime',
    width: 120,
    align: useStore.isCenter
  },
  {
    title: '审批状态',
    key: 'fromStatus',
    width: 50,
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    align: useStore.isCenter,
    width: 130,
    fixed: 'right',
    render: (row: any) => [
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => {
            router.push({ path: '/OA/Reception/ReceptionInfo', query: { id: row.id } })
          }
        },
        {
          default: () => '详情'
        }
      ),
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          style: { display: row.fromStatus == '审批中' ? 'inlne-block' : 'none' },
          onClick: async () => {
            recallId.value = row.id
            modalVisible.value = true
          }
        },
        {
          default: () => '撤回'
        }
      ),
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => {
            router.push({
              path: '/OA/Reception/ReceptionUpdate',
              query: { id: row.id, flat: true as never }
            })
          }
        },
        {
          default: () => '再次发起'
        }
      )
    ]
  }
]
// 时间选择
const handleFormattedStart = (v: any) => {
  if (v) (paginationReactive.accessStartTime = v[0]), (paginationReactive.accessEndTime = v[1])
  else (paginationReactive.accessStartTime = null), (paginationReactive.accessEndTime = null)
}
// 上一页下一页
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  accessStartTime: null, //开始时间
  accessEndTime: null, //结束时间
  accessMan: null, //访问单位/个人
  duty: null, //带队领导/职务
  contacts: null, //联系人姓名
  phone: null, //联系人联系方式
  onChange: (page: number) => {
    paginationReactive.page = page
    getReceptionList()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    getReceptionList()
  }
})
// 撤回
const recallId = ref()
const recall = async () => {
  modalVisible.value = false
  tableLoading.value = true
  const [error, res] = await sendRequest<HttpResponse<ReceptionModel>>({
    module: 'receiveOrDispatch',
    api: 'dispatchRevoke',
    override: {
      data: {
        functionCode: 'A0536',
        resourceId: recallId.value
      }
    }
  })
  if (error) return message.error('撤回失败'), (tableLoading.value = false)
  if (res.code !== 0) return message.error('撤回失败'), (tableLoading.value = false)
  getReceptionList()
  message.success('撤回成功')
}
// 获取列表
const tableData = ref([])
const tableLoading = ref(false)
const getReceptionList = async () => {
  tableLoading.value = true
  const [error, res] = await sendRequest<HttpResponse<ReceptionModel>>({
    module: 'reception',
    api: 'receptionList',
    override: {
      params: {
        page: paginationReactive.page,
        limit: paginationReactive.pageSize,
        accessStartTime: paginationReactive.accessStartTime,
        accessEndTime: paginationReactive.accessEndTime,
        accessMan: paginationReactive.accessMan,
        duty: paginationReactive.duty,
        contacts: paginationReactive.contacts,
        phone: paginationReactive.phone
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  tableData.value = []
  setTimeout(() => {
    tableData.value = res.data.list
    paginationReactive.itemCount = res.data.totalCount
  }, 0)
}

watch(
  () => route,
  () => {
    if (route.path == '/OA/Reception/reception') getReceptionList()
  },
  { deep: true, immediate: true }
)
</script>
<style scoped lang="scss">
.table {
  padding: 29rem 30rem 42rem;
}
</style>
